import type { RouteRecordRaw } from 'vue-router'

export let firstRoute: RouteRecordRaw | undefined = undefined

/**
 * 映射菜单到路由
 * @param menus 菜单
 * @returns 路由
 */
export function mapMenuToRoutes(menus: any[]) {
  // 1.加载所有的路由对象
  const localRoutes = loadLocalRoutes()

  // 2.路由匹配
  const finalRoutes: RouteRecordRaw[] = []
  // for (const menu of menus) {
  //   if (menu.type === 2 || menu.type === 3) {
  //     // type == 2 代表无children
  //     const menuUrl = menu.url
  //     const route = localRoutes.find((item) => item.path === menuUrl)
  //     if (route) {
  //       finalRoutes.push(route)
  //     }
  //   } else if (menu.type === 1) {
  //     // type == 1 代表有children
  //     for (const submenu of menu.children) {
  //       const menuUrl = submenu.url
  //       const route = localRoutes.find((item) => item.path === menuUrl)
  //       if (route) {
  //         finalRoutes.push(route)
  //       }
  //     }
  //   }
  // }

  // 3.不确定有几层
  function _recurseGetRoute(menus: any[]) {
    for (const menu of menus) {
      if (menu.type === 2 || menu.type === 3) {
        // type == 2 代表无children 注册路由 注册进入侧边栏
        // type == 3 代表无children 注册路由 不注册侧边栏
        const route = localRoutes.find((item) => item.path === menu.url)
        if (route) finalRoutes.push(route)
        if (!firstRoute && route) firstRoute = route
      } else if (menu.type === 1) {
        _recurseGetRoute(menu.children ?? [])
      }
    }
  }

  _recurseGetRoute(menus)

  return finalRoutes
}

// 加载所有的路由对象
function loadLocalRoutes() {
  // 1.加载所有的模板
  const modules: Record<string, any> = import.meta.glob('../router/main/**/*.ts', { eager: true })

  // 2.遍历所有的模板为路由对象
  const routes: RouteRecordRaw[] = []
  for (const key in modules) {
    const route = modules[key].default
    routes.push(route)
  }

  return routes
}

export function mapMenuToPersssions(menus: any[]) {
  const permissions: string[] = []
  function _recurseGetPermission(menuList: any[]) {
    for (const menu of menuList) {
      if (menu.type === 1 || menu.type === 2) {
        _recurseGetPermission(menu.children ?? [])
      } else {
        permissions.push(menu.permission)
      }
    }
  }
  _recurseGetPermission(menus)
  return permissions
}

// tag==id rank==type 处理menu格式
export const transform_menu = (menus: any) => {
  const fn = (menuItem: any) => {
    const children: any = menuItem.sysMenuNewList ? transform_menu(menuItem.sysMenuNewList) : null
    return {
      sort: menuItem.sort,
      url: menuItem.url,
      type: menuItem.rank,
      name: menuItem.name || '',
      id: menuItem.tag,
      uid: menuItem.uid,
      icon: menuItem.icon || '',
      children
    }
  }

  const newMenu = menus.map((menu: any) => {
    return fn(menu)
  })

  return newMenu
}

// 根据path查找menu (两种递归方式)
// export const mapPathToMenu: any = (path: string, userMenus: any[]) => {
//   for (const menu of userMenus) {
//     if (menu.url === path) {
//       return menu
//     }

//     if (menu.children) {
//       const currMenu = mapPathToMenu(path, menu.children)
//       if (currMenu) return currMenu
//     }
//   }
// }

// 根据path查找menu (两种递归方式)
export const mapPathToMenu: any = (paths: string, userMenusList: any[]) => {
  let currMenu = null

  function _mapPathToMenu(path: string, userMenus: any[]) {
    for (const menu of userMenus) {
      if (menu.url === path) {
        currMenu = menu
      }

      if (menu.children) {
        _mapPathToMenu(path, menu.children)
      }
    }
  }
  _mapPathToMenu(paths, userMenusList)

  return currMenu
}
